package com.example.comitclx1.controller;

import com.example.comitclx1.anno.Log;
import com.example.comitclx1.mapper.EmpMapper;
import com.example.comitclx1.pojo.Emp;
import com.example.comitclx1.pojo.PageBean;
import com.example.comitclx1.pojo.Result;
import com.example.comitclx1.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    /**
     * 查询分页总页数
     * @return
     */
    @Autowired
    private EmpService empService;
    @GetMapping
    public Result Page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("你好啊");
      PageBean pageBean= empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    /**
     * 删除操作
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除操作");
        empService.delete(ids);
        return Result.success();
    }
/**
 * 插入数据
 */
    @Log
   @PostMapping
    public Result inset(@RequestBody Emp emp){
       empService.inset(emp);
        return Result.success();
    }
    /**
     * 根据id查询数据
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        Emp emp= empService.getById(id);
        return Result.success(emp);
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp){
       empService.update(emp);
        return Result.success();
    }
}
